package top.superflow.flow.runtime.mapper

import top.superflow.core.utils.PojoUtils
import top.superflow.lang.CollectionUtils
import top.superflow.log.Loggable

import scala.collection.mutable

object ValueExtractUtils extends Loggable{

  def extract (any : Any @unchecked, expectedParamName : String) : Any = {
    any match {
      case map : Map[String, _] =>{
        val optionalValue = map.get(expectedParamName)
        if(optionalValue.isEmpty){
          return null
        }

        optionalValue.get
      }
      case _ => {
        return PojoUtils.getField(any, expectedParamName)
      }
    }
  }

  def extract (any : Any, paramNameQueue : mutable.Queue[String]) : Any = {
    if(CollectionUtils.isEmpty(paramNameQueue)){
      return any
    }

    val firstParamName = paramNameQueue.dequeue()
    val valueForFirstParamName = extract(any, firstParamName)

    return extract(valueForFirstParamName, paramNameQueue)
  }

}
